#!/bin/bash

# Check if sq_task_pids file is provided


# 定义排序函数
sort_pids_by_dev() {
    local input_file="$1"
    local output_file="$2"
    
    # 参数检查
    if [[ -z "$input_file" || -z "$output_file" ]]; then
        echo "Usage: sort_pids_by_dev <input_file> <output_file>"
        return 1
    fi
    
    # 检查输入文件是否存在
    if [[ ! -f "$input_file" ]]; then
        echo "Error: Input file '$input_file' not found"
        return 1
    fi
    
    # 处理文件内容：
    # 1. 使用awk提取设备号和整行内容
    # 2. 按设备号排序
    # 3. 提取第一列(PID)
    awk '{
        # 提取设备号
        match($2, /dev([0-9]+)_sq_task/, arr)
        if (length(arr[1]) > 0) {
            dev_num = int(arr[1])
            print dev_num, $0
        }
    }' "$input_file" | \
    sort -n -k1,1 | \
    awk '{print $2}' > "$output_file"
    
    echo "排序完成，结果已写入 $output_file"
}


ps -ef | grep _sq_task | awk '{if (NR>1) print prev; prev=$0;}' | awk '{print $2, $8}' > tmp.txt

sort_pids_by_dev "tmp.txt" "tmp2.txt"


SQ_TASK_PIDS_FILE=tmp2.txt

# Check if the file exists
if [ ! -f "$SQ_TASK_PIDS_FILE" ]; then
    echo "Error: File $SQ_TASK_PIDS_FILE not found!"
    exit 1
fi

decimal_to_256bit_hex_mask() {
    local decimal_input="$1"

    # --- 参数验证 ---
    if [ $# -ne 1 ]; then
        echo "错误 (函数): 请提供一个参数。" >&2
        return 1
    fi

    if ! [[ "$decimal_input" =~ ^[0-9]+$ ]]; then
        echo "错误 (函数): '$decimal_input' 不是一个有效的非负整数。" >&2
        return 1
    fi

    if [ "$decimal_input" -lt 0 ] || [ "$decimal_input" -gt 255 ]; then
        echo "错误 (函数): 输入值 $decimal_input 超出范围 (0-255)。" >&2
        return 1
    fi

    # --- 核心计算逻辑 ---
    local BITS_PER_GROUP=32
    local NUM_GROUPS=8

    # 计算组索引 (从左到右 0-7) 和组内位索引 (0-31)
    local group_index=$((decimal_input / BITS_PER_GROUP))
    local bit_position_in_group=$((decimal_input % BITS_PER_GROUP))

    # 计算该组的十进制值 (2^bit_position_in_group)
    local group_value_decimal=$((1 << bit_position_in_group))

    # 将十进制值转换为8位十六进制 (大写)
    local group_value_hex
    group_value_hex=$(printf "%08X" "$group_value_decimal")

    # --- 构建并输出掩码 ---
    local output_mask=""
    local i

    for (( i=0; i<NUM_GROUPS; i++ )); do
        if [ $i -eq $((NUM_GROUPS - 1 - group_index)) ]; then
            # 注意组的顺序
            output_mask="${output_mask}${group_value_hex}"
        else
            output_mask="${output_mask}00000000"
        fi

        # 添加逗号分隔符（除了最后一个）
        if [ $i -lt $((NUM_GROUPS - 1)) ]; then
            output_mask="${output_mask},"
        fi
    done

    echo "$output_mask"
    return 0
}


# Initialize cpu-no with the base value
CPU_NO=193
STEP=4

# Process each PID in the sq_task_pids file
while IFS= read -r sq_task_pid; do
    # Skip empty lines
    if [ -n "$sq_task_pid" ]; then
        echo "Executing: taskset -pc $CPU_NO $sq_task_pid"
        taskset -pc $CPU_NO $sq_task_pid
        # Increment cpu-no
        CPU_NO=$((CPU_NO + STEP))
    fi
done < "$SQ_TASK_PIDS_FILE"

# Get all interrupt numbers for trs-mb
echo "Getting interrupt numbers for trs-mb..."
IRQ_NUMBERS=$(cat /proc/interrupts | grep trs-mb | awk -F ":" '{print $1}' | tr -d ' ')

IRQ_CPU=194
SQ_CPU=4

# Process each interrupt number
for irq_no in $IRQ_NUMBERS; do
    echo "Executing: echo $IRQ_CPU > /proc/irq/$irq_no/smp_affinity_list"
    echo $IRQ_CPU > /proc/irq/$irq_no/smp_affinity_list
    # Increment cpu-no
    IRQ_CPU=$((IRQ_CPU + STEP))
done

# Process dev-no from 1 to 16
for dev_no in $(seq 0 15); do
    # Execute for _sq_send_wq
    cpu_mask=$(decimal_to_256bit_hex_mask $SQ_CPU)

    echo "Executing: echo $cpu_mask > /sys/devices/virtual/workqueue/dev${dev_no}_sq_send_wq/cpumask"
    echo $cpu_mask > /sys/devices/virtual/workqueue/dev${dev_no}_sq_send_wq/cpumask
    
    # Execute for _sq_fair_wq
    echo "Executing: echo $cpu_mask > /sys/devices/virtual/workqueue/dev${dev_no}_sq_fair_wq/cpumask"
    echo $cpu_mask > /sys/devices/virtual/workqueue/dev${dev_no}_sq_fair_wq/cpumask
    
    # Increment cpu-no
    SQ_CPU=$((SQ_CPU + STEP))
done

echo "Script execution completed."




